<?php
namespace Admin\Controller;
use Admin\Model\TagModel;
use Think\Page;
use Admin\Model\FileModel;
use Admin\Model\ProjectModel;
use Org\Util\SFile;

/**
 * 案例列表操作
 *
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2018-6-1 17:39:28
 */
class ProjectController extends BashController{
    /**
     * 案例列表
     * @return type
     */
    public function listAction(){
        $tag = new TagModel();
        $file = new FileModel();
        $project = new ProjectModel();
        $where = array();
        foreach(explode(' ', I('get.keywords')) as $v){
            $where[] = '%'.$v.'%';
        }
        $where = array(
            'name' => array('like',$where),
        );
        $where['type'] = get_config_id('TagType', '案例');
        $count = $tag->where($where)->getCount();
        $page = new Page($count, 3);// 修改分页列表，使用卡片模式展现案例列表
        $list = $tag->where($where)->getList($page->firstRow, $page->listRows);
        foreach($list as $k => $v){
//            注释掉该行，修改板式为案例+产品形式
//            $list[$k]['img_link'] = $file->get_img_all('tag',$v['id']);
            $list[$k]['project'] = $tag->get_link()->where(array(
                'file_id' => $v['id'],
                'target_name' => $tag->get_table_id('project'),
            ))->getList();
            foreach($list[$k]['project'] as $kk => $vv){
                $list[$k]['project'][$kk]['info'] = $project->get_info($vv['target_id']);
                $list[$k]['project'][$kk]['file'] = $file->get_img_all('project',$vv['target_id']);
            }
        }
        return $this->assign(array(
            'count' => $count,
            'list' => $list,
            'page' => $page->show(),
        ))->display();
    }
    
    /**
     * 修改案例信息
     * @return type
     */
    public function saveAction(){
        $tag = new TagModel();
        $this->wget('bootstrap-select2');
        $info = $tag->get_info(I('get.id'));
        if(empty($info)){
            $this->error('参数错误');
        }
        $file = new FileModel();
        $project = new ProjectModel();
        if(!I('post.')){
            $file->call_back(I('get.id'), 'tag');// 回滚删除的图片
            $tag->set_delete_reback('project',I('get.id'),true);// 回滚删除的产品
            // 因为删除的产品是产品和标签关联，所以此处删除的表明为project
            // 获取本标签下关联的产品
            $project_list = $tag->get_link()->where(array(
                'file_id' => I('get.id'),
                'target_name' => $tag->get_table_id('project'),
                'status' => 99,
            ))->getList(0,99);
            foreach($project_list as $k => $v){
                $project_list[$k]['project_info'] = $project->get_info($v['target_id']);
            }
            $file_link = $file->get_link()->where(array(
                'target_id' => I('get.id'),
                'target_name' => $tag->get_table_id('tag'),
            ))->getList(0,99);
            foreach($file_link as $k => $v){
                $file_link[$k]['file_content'] = $file->get_info($v['file_id']);
            }
            $this->assign('info',$info);
            $this->assign('file_link',$file_link);
            $this->assign('project_list',$project_list);
            $this->assign('project_list_all',$project->getList(0,99));
            $this->assign('_new',true);
            return $this->display('save');
        }
        // 如果是提交的数据
        // 先进行图片提交
        $sfile = new SFile();
        $sfile->rm_dirs(RUNTIME_PATH);
        $file->submit_back(I('get.id'), 'tag');
        $tag->set_delete_submit('project',I('get.id'));
        $file_content = $this->Upload('file');
        foreach($file_content as $v){
            $id = $file->check_info($v);
            $file->get_link()->create_info(array(
                'target_name' => $file->get_table_id('tag'),
                'file_id' => $id,
                'target_id' => I('get.id'),
            ));
        }
        $post = I('post.');
        $post['status'] = 99;
        $tag->where(array('id'=>I('get.id')))->save($post);
        return $this->success('保存成功',U('list'));
    }
    
    /**
     * 删除品牌
     * @return type
     */
    public function brand_delAction(){
        if(IS_AJAX){
            $id = I('get.id');
            $tag = new TagModel();
            $tag->setDelete($id);
//            if($tag->setDelete($id)){
            return $this->success('删除成功',U('brand_list'));
//            }
//            return $this->error('系统错误');
        }
        return $this->error('系统错误',U('Index/index'));
    }
    
    /**
     * 添加案例操作
     * @return type
     */
    public function addAction(){
        $tag = new TagModel();
        $this->wget('bootstrap-select2');
        if(!I('get.id')){
            // 如果是新建品牌则直接创建一个已删除的品牌进行添加
            $id = $tag->create_info(array(
                'status'=>98,
                'type' => get_config_id('TagType','案例'),
            ));
            if($id){
                (new \Common\Model\LogModel())->create_oth_log($id, 'tag');
            }
            $this->redirect('Project/add',array('id'=>$id));
        }
        $info = $tag->get_info(I('get.id'));
        if(empty($info)){
            $this->error('参数错误');
        }
        $file = new FileModel();
        $project = new ProjectModel();
        if(!I('post.')){
            $file->call_back(I('get.id'), 'tag');// 回滚删除的图片
            $tag->set_delete_reback('project',I('get.id'),true);// 回滚删除的产品
            // 因为删除的产品是产品和标签关联，所以此处删除的表明为project
            // 获取本标签下关联的产品
            $project_list = $tag->get_link()->where(array(
                'file_id' => I('get.id'),
                'target_name' => $tag->get_table_id('project'),
                'status' => 99,
            ))->getList(0,99);
            foreach($project_list as $k => $v){
                $project_list[$k]['project_info'] = $project->get_info($v['target_id']);
            }
            $file_link = $file->get_link()->where(array(
                'target_id' => I('get.id'),
                'target_name' => $tag->get_table_id('tag'),
            ))->getList(0,99);
            foreach($file_link as $k => $v){
                $file_link[$k]['file_content'] = $file->get_info($v['file_id']);
            }
            $this->assign('info',$info);
            $this->assign('file_link',$file_link);
            $this->assign('project_list',$project_list);
            $this->assign('project_list_all',$project->getList(0,99));
            $this->assign('_new',true);
            return $this->display('save');
        }
        // 如果是提交的数据
        // 先进行图片提交
        $sfile = new SFile();
        $sfile->rm_dirs(RUNTIME_PATH);
        $file->submit_back(I('get.id'), 'tag');
        $tag->set_delete_submit('project',I('get.id'));
        $file_content = $this->Upload('file');
        foreach($file_content as $v){
            $id = $file->check_info($v);
            $file->get_link()->create_info(array(
                'target_name' => $file->get_table_id('tag'),
                'file_id' => $id,
                'target_id' => I('get.id'),
            ));
        }
        $post = I('post.');
        $post['status'] = 99;
        $tag->where(array('id'=>I('get.id')))->save($post);
        return $this->success('保存成功',U('list'));
    }
}